import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/provider/group_formdate_model.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';

class WriteGroupName extends StatefulWidget {
  WriteGroupName({Key key}) : super(key: key);

  @override
  _WriteGroupNameState createState() => _WriteGroupNameState();
}

class _WriteGroupNameState extends State<WriteGroupName> {
  TextEditingController _editController = TextEditingController();
  GroupFormDataModel _formData;

  String _parseTitles(name) {
    String title = '';
    if (name == 'name') {
      title = "小组名称";
    } else if (name == 'summary') {
      title = "小组简介";
    } else if (name == 'note') {
      title = "小组通知";
    } else if (name == 'webLink') {
      title = "小组网址";
    }
    return title;
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _formData = Provider.of<GroupFormDataModel>(context);
      String name = ModalRoute.of(context).settings.arguments;
      _formData.setValue(name);
      _editController.text = _formData.value ?? '';
    });
  }

  @override
  Widget build(BuildContext context) {
    String name = ModalRoute.of(context).settings.arguments;
    if (_formData.myGroup == null) return G.emptyBox();
    return Scaffold(
      appBar: AppBar(
        title: Text('编辑${_parseTitles(name)}'),
        actions: <Widget>[
          GestureDetector(
              onTap: _saveFormData,
              child: _formData.myGroup.owner
                  ? Row(children: [
                      Container(
                        margin: EdgeInsets.only(right: 10),
                        padding:
                            EdgeInsets.symmetric(horizontal: 20, vertical: 4),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(2),
                          color: rgba(42, 125, 250, 1),
                        ),
                        child: Text(
                          '确认',
                          style: TextStyle(
                            color: rgba(255, 255, 255, 1),
                          ),
                        ),
                      )
                    ])
                  : G.emptyBox())
        ],
      ),
      body: Container(
        color: rgba(255, 255, 255, 1),
        padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        child: TextField(
          controller: _editController,
          maxLines: null,
          decoration: InputDecoration(
            hintText: '${_parseTitles(name)}',
            border: InputBorder.none,
          ),
        ),
      ),
    );
  }

// 保存输入信息
  _saveFormData() {
    String name = ModalRoute.of(context).settings.arguments;
    GroupFormDataModel _formData = Provider.of<GroupFormDataModel>(context);
    String content = _editController.text;
    _formData.setName("$content", name);
    G.pop();
  }
}
